package com.haoshuang.zhouzhoubang.ui.page.mine.info

import android.content.Context
import android.os.Bundle
import androidx.core.widget.addTextChangedListener
import com.haoshuang.zhouzhoubang.databinding.ActivityMineUserInfoBinding
import com.haoshuang.zhouzhoubang.utils.MultiMediaChooseUtils
import com.haoshuang.zhouzhoubang.utils.OSSUploadUtils
import com.haoshuang.zhouzhoubang.widget.CustomUploadView
import com.haoshuang.zhouzhoubang.widget.UploadFileBean
import com.lnkj.jialubao.utils.AccountUtils
import com.lnkj.jialubao.utils.AccountUtils.user
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.load

/**
 *  门店信息
 */
class MineUserInfoActivity : BaseVMActivity<MineUserInfoViewModel, ActivityMineUserInfoBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<MineUserInfoActivity>()
        }
    }

    private var logo = ""
    private var companyPic = ""

    override fun initView(savedInstanceState: Bundle?) {
        binding.titleBar.title = "门店信息"
        binding.titleBar.leftImageView().click { finish() }

        binding.apply {
            val team = AccountUtils.user?.team
            etName.setText(team?.team_name)
            logo = team?.team_logo ?: ""
            rivHead.setSingleMedia(team?.team_logo)
            ivBusinessLicense.setSingleMedia(user?.team_company_pic)
            companyPic = user?.team_company_pic ?: ""
            etContent.setText(user?.synopsis)
            tvCount.text = "${etContent.text.toString().length}/200"
            etContent.addTextChangedListener {
                tvCount.text = "${etContent.text.toString().length}/200"
            }

            rivHead.setOnUploadResultListener(object: CustomUploadView.OnUploadResultListener {
                override fun onUploadResult(mediaList: List<UploadFileBean>) {
                    logo = mediaList[0].url?:""
                    rivHead.setSingleMedia(logo)
                }
            })
            ivBusinessLicense.setOnUploadResultListener(object: CustomUploadView.OnUploadResultListener {
                override fun onUploadResult(mediaList: List<UploadFileBean>) {
                    companyPic = mediaList[0].url?:""
                    ivBusinessLicense.setSingleMedia(companyPic)
                }
            })

            tvCommit.clickWithTrigger {
                val name = etName.text.toString()
                val content = etContent.text.toString()
                if (name.isEmpty()) {
                    toast("门店名称不能为空")
                    return@clickWithTrigger
                }
                if (logo.isEmpty()) {
                    toast("请上传门店头像")
                    return@clickWithTrigger
                }
                if (companyPic.isEmpty()) {
                    toast("请上传营业执照")
                    return@clickWithTrigger
                }
                if (content.isEmpty()) {
                    toast("门店简介不能为空")
                    return@clickWithTrigger
                }
                vm.updateTeamInfo(name,  logo, companyPic, content)
            }
        }


    }

    override fun initData() {

    }

    override fun startObserve() {
        vm.updateTeamInfoData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                setResult(200)
                finish()
            }
        }
    }

}